package com.user;

import com.utils.JdbcUtils;
import com.utils.JsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "ChangeUser", urlPatterns = {"/ChangeUser"})
public class ChangeUser extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String userId = (String) session.getAttribute("id");

        String name = new String(req.getParameter("Name").getBytes("iso-8859-1"), "utf-8");
        String userGender = new String(req.getParameter("UserGender").getBytes("iso-8859-1"), "utf-8");
        String userPhone = req.getParameter("UserPhone");
        String userEmail = req.getParameter("UserEmail");
        String userPosition = req.getParameter("UserPosition");
//        String userSystemPermission = req.getParameter("userSystemPermission");
        String sql = "update animalfile09_systemuser set Name=?,UserGender=?," +
                "UserPhone=?,UserEmail=?,UserPosition=? where UserID = ?";
        Connection con = JdbcUtils.getCon();
        PreparedStatement stat = null;
        try {
            stat = con.prepareStatement(sql);
            stat.setString(1, name);
            stat.setString(2, userGender);
            stat.setString(3, userPhone);
            stat.setString(4, userEmail);
            stat.setString(5, userPosition);
//            stat.setString(6, userSystemPermission);
            stat.setString(6, userId);
            stat.executeUpdate();
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().println("<html>" +
                    "<center><h4>修改成功</h4></center>" +
                    "</html>");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            resp.getWriter().println("修改失败");
        } finally {
            JdbcUtils.close2(con, stat);
        }
    }
}
